<template>
  <div class="mod-distribution-prod">
    <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               v-model="dataForm"
               :table-loading="dataListLoading"
               :option="tableOption"
               @search-change="searchChange"
               @refresh-change="refreshChange"
               @on-load="getDataList"
               @selection-change="selectionChange">
      <template slot-scope="scope"
                slot="prodName">
        {{scope.row.product.prodName}}
      </template>

      <template slot-scope="scope"
                slot="pic">
        <img :src="scope.row.product.pic"
             width="100"
             height="100" />
      </template>

      <template slot-scope="scope"
                slot="state">
        <el-tag v-if="scope.row.state === 0"
                size="small">{{$t("publics.metLowerShelf")}}</el-tag>
        <el-tag v-if="scope.row.state === 1"
                size="small"
                type="success">{{$t("publics.metUpperShelf")}}</el-tag>
        <el-tag v-else-if="scope.row.state ===2"
                size="small"
                type="danger">{{$t("groups.offlineViolation")}}</el-tag>
        <el-tag v-else-if="scope.row.state ===3"
                size="small"
                type="warning">{{$t("product.pendingReview")}}</el-tag>
      </template>

      <template slot-scope="scope"
                slot="menuLeft">
        <el-button type="primary"
                   icon="el-icon-plus"
                   size="small"
                   v-if="isAuth('distribution:distributionProd:save')"
                   @click.stop="addOrUpdateHandle(scope.row)">{{$t("crud.addTitle")}}</el-button>

        <el-button type="danger"
                   size="small"
                   v-if="isAuth('distribution:distributionProd:delete')"
                   @click.stop="deleteHandle"
                   :disabled="dataListSelections.length <= 0">{{$t("sys.batchDelete")}}</el-button>
      </template>

      <template slot-scope="scope"
                slot="menu">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-edit"
                   v-if="isAuth('distribution:distributionProd:update')"
                   @click="addOrUpdateHandle(scope.row)">{{$t("crud.updateBtn")}}</el-button>
        <el-button type="primary"
                   icon="el-icon-document-checked"
                   size="small"
                   v-if="isAuth('distribution:distributionProd:auditApply') && scope.row.state > 1"
                   @click="auditEventHandle(scope.row.distributionProdId)">{{$t("product.offlineManagement")}}</el-button>
        <el-button type="danger"
                   icon="el-icon-delete"
                   size="small"
                   v-if="isAuth('distribution:distributionProd:delete')"
                   @click="deleteHandle(scope.row)">{{$t("text.delBtn")}}</el-button>
      </template>

    </avue-crud>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible"
                   ref="addOrUpdate"
                   @refreshDataList="refreshChange"></add-or-update>

    <!-- 下线管理弹窗-->
    <offlineEventHandle v-if="offlineEventHandleVisible"
                        selectUrl="/distribution/distributionProd/getOfflineEventByDistProdId"
                        applyUrl="/distribution/distributionProd/auditApply"
                        ref="offlineEvent"
                        @refreshDataList="refreshChange"></offlineEventHandle>
  </div>
</template>

<script>
import { tableOption } from '@/crud/marketing/distribution/distributionProd'
import AddOrUpdate from './distribution-prod-add-or-update'
import OfflineEventHandle from '@/components/offline-event-handle'

export default {
  data () {
    return {
      dataForm: {
        prodName: ''
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      searchForm: {},
      dataList: [],
      tableOption: tableOption,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      offlineEventHandleVisible: false
    }
  },
  components: {
    AddOrUpdate,
    OfflineEventHandle
  },
  methods: {
    // 多选回调
    selectionChange (list) {
      this.dataListSelections = list
    },
    // 新增 / 修改
    addOrUpdateHandle (data) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data)
      })
    },
    // 点击查询
    searchChange (params) {
      this.searchForm = params
      this.page.currentPage = 1
      this.page.pageSize = 10
      this.getDataList(this.page)
    },
    // 刷新回调
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    // 获取数据列表
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/distribution/distributionProd/page'),
        method: 'get',
        params: this.$http.adornParams(Object.assign({
          current: page ? page.currentPage : 1,
          size: page ? page.pageSize : 20
        }, this.searchForm))
      }).then(({ data }) => {
        this.page.total = data.total
        this.page.pageSize = data.size
        this.page.currentPage = data.current
        this.dataList = data.records
        this.dataListLoading = false
      })
    },
    // 删除
    deleteHandle (row, index) {
      var ids = row.distributionProdId ? [row.distributionProdId] : this.dataListSelections.map(item => {
        return item.distributionProdId
      })
      this.$confirm(`${this.$i18n.t('sys.makeSure')}[${row.distributionProdId ? this.$i18n.t('text.delBtn') : this.$i18n.t('sys.batchDelete')}]${this.$i18n.t('text.menu')}?`, this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/distribution/distributionProd'),
          method: 'delete',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      })
    },
    // 下线管理
    auditEventHandle (id) {
      this.offlineEventHandleVisible = true
      this.$nextTick(() => {
        this.$refs.offlineEvent.init(id)
      })
    }
  }
}
</script>
